#include "host.h"
#include <math.h>

void handler_sv(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_sv(arg_value);
    if(arg_value != (rt_uint16_t)host_data->sv)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->sv;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_tempAlarmLow(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_tempAlarmLow(arg_value);
    if(arg_value != (rt_uint16_t)host_data->tempAlarmLow)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->tempAlarmLow;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_tempAlarmHigh(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_tempAlarmHigh(arg_value);
    if(arg_value != (rt_uint16_t)host_data->tempAlarmHigh)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->tempAlarmHigh;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_at(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_atEnable(arg_value);
    if(arg_value != (rt_uint16_t)host_data->atEnable)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->atEnable;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_tecCtrlMode(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_tecCtrlMode(arg_value);
    if(arg_value != (rt_uint16_t)host_data->tecCtrlMode)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->tecCtrlMode;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_tecPower(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    if(host_data->tecCtrlMode == TEC_Ctrl_Mode_Hand)
    {
        host_data_set_tecPower(arg_value);
    }
    if(arg_value != (rt_uint16_t)host_data->tecPower)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->tecPower;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_workMode(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_workMode(arg_value);
    if(arg_value != (rt_uint16_t)host_data->workMode)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->workMode;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_pidKp(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_pidKp(arg_value);
    if(arg_value != (rt_uint16_t)host_data->pidKp)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->pidKp;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_pidKi(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_pidKi(arg_value);
    if(arg_value != (rt_uint16_t)host_data->pidKi)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->pidKi;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_pidKd(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_pidKd(arg_value);
    if(arg_value != (rt_uint16_t)host_data->pidKd)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->pidKd;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_svOffset(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_svOffset(arg_value);
    if(arg_value != (rt_uint16_t)host_data->svOffset)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->svOffset;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_pvOffset(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_pvOffset(arg_value);
    if(arg_value != (rt_uint16_t)host_data->pvOffset)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->pvOffset;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_tecOutputDir(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_tecOutputDir(arg_value);
    if(arg_value != (rt_uint16_t)host_data->tecOutputDir)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->tecOutputDir;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_tempSensorType(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_tempSensorType(arg_value);
    if(arg_value != (rt_uint16_t)host_data->tempSensorType)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->tempSensorType;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_tempUnit(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_tempUnit(arg_value);
    if(arg_value != (rt_uint16_t)host_data->tempUnit)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->tempUnit;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_decimalPointNbr(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg[11];
    rt_uint8_t msg_len = 0;
    rt_int8_t factor;
    rt_int16_t value;

    factor = arg_value - host_data->decimalPointNbr;
    host_data_set_decimalPointNbr(arg_value);
    if(arg_value != (rt_uint16_t)host_data->decimalPointNbr)
    {
        msg[0].arg_id = arg_id;
        msg[0].arg_value = host_data->decimalPointNbr;
        rt_mq_send(mq_comm, msg, sizeof(msg[0]));
    }
    else
    {
        value = host_data->sv * pow(10, factor);;
        host_data_set_sv(value);
        msg[msg_len].arg_id = ARG_ID_SV;
        msg[msg_len++].arg_value = host_data->sv;
        value = host_data->tempAlarmLow * pow(10, factor);;
        host_data_set_tempAlarmLow(value);
        msg[msg_len].arg_id = ARG_ID_TempAlarmLow;
        msg[msg_len++].arg_value = host_data->tempAlarmLow;
        value = host_data->tempAlarmHigh * pow(10, factor);
        host_data_set_tempAlarmHigh(value);
        msg[msg_len].arg_id = ARG_ID_TempAlarmHigh;
        msg[msg_len++].arg_value = host_data->tempAlarmHigh;
        for(rt_uint8_t i = 0; i < 8; i++)
        {
            value = host_data->progList[i].stepSv * pow(10, factor);
            host_data_set_progStepSv(i, value);
            msg[msg_len].arg_id = ARG_ID_ProgStep1Sv + i * 6;
            msg[msg_len++].arg_value = host_data->progList[i].stepSv;
        }
        rt_mq_send(mq_comm, msg, sizeof(msg));
    }
}
void handler_displayRangeLow(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_displayRangeLow(arg_value);
    if(arg_value != (rt_uint16_t)host_data->displayRangeLow)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->displayRangeLow;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_displayRangeHigh(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_displayRangeHigh(arg_value);
    if(arg_value != (rt_uint16_t)host_data->displayRangeHigh)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->displayRangeHigh;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_filterDegree(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_filterDegree(arg_value);
    if(arg_value != (rt_uint16_t)host_data->filterDegree)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->filterDegree;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_mbSlaverAddr(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_mbSlaverAddr(arg_value);
    if(arg_value != (rt_uint16_t)host_data->mbSlaverAddr)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->mbSlaverAddr;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_mbBaudrateID(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_mbBaudrateID(arg_value);
    if(arg_value != (rt_uint16_t)host_data->mbSlaverAddr)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->mbSlaverAddr;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_progTimeUnitAdjust(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
}
void handler_progTimeUnitHold(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
}
void handler_progStart(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
}
void handler_progTempAcceptance(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
}
void handler_progStepAdjustTime(rt_uint8_t arg_id, rt_uint16_t arg_value)
{

}
void handler_progStepSv(rt_uint8_t arg_id, rt_uint16_t arg_value)
{

}
void handler_progStepHoldTime(rt_uint8_t arg_id, rt_uint16_t arg_value)
{

}
void handler_progStepMode(rt_uint8_t arg_id, rt_uint16_t arg_value)
{

}
void handler_progStepNextStepNbr(rt_uint8_t arg_id, rt_uint16_t arg_value)
{

}
void handler_progStepLoopCnt(rt_uint8_t arg_id, rt_uint16_t arg_value)
{

}
void handler_powerOnAutoRun(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
}
void handler_steadyAlarmRange(rt_uint8_t arg_id, rt_uint16_t arg_value)
{

}
void handler_tecMaxPower(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_tecMaxPower(arg_value);
    if(arg_value != (rt_uint16_t)host_data->tecMaxPower)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->tecMaxPower;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_mbCrcEnable(rt_uint8_t arg_id, rt_uint16_t arg_value)
{

}
void handler_pidMode(rt_uint8_t arg_id, rt_uint16_t arg_value)
{

}
void handler_tempCalibReal(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;
    rt_uint8_t index = arg_id - ARG_ID_TempCalibReal0;

    host_data_set_tempCalibReal(index, arg_value);
    if(arg_value != (rt_uint16_t)host_data->tempCalibReal[index])
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->tempCalibReal[index];
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_tempCalibSet(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;
    rt_uint8_t index = arg_id - ARG_ID_TempCalibSet0;

    host_data_set_tempCalibSet(index, arg_value);
    if(arg_value != (rt_uint16_t)host_data->tempCalibSet[index])
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->tempCalibSet[index];
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_tempCalibPointNbr(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    struct Msg_InterThread msg;

    host_data_set_tempCalibPointNbr(arg_value);
    if(arg_value != (rt_uint16_t)host_data->tempCalibPointNbr)
    {
        msg.arg_id = arg_id;
        msg.arg_value = host_data->tempCalibPointNbr;
        rt_mq_send(mq_comm, &msg, sizeof(msg));
    }
}
void handler_reserve(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
}
void handler_factory(rt_uint8_t arg_id, rt_uint16_t arg_value)
{
    if(arg_value == 1)
    {
        host_factory();
    }
}